package com.yc.supermarket.web.controller;



import com.yc.supermarket.entity.Report;
import com.yc.supermarket.service.IReportService;
import com.yc.supermarket.service.impl.ReportServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/report")
public class ReportController extends BaseController{
    private IReportService service=new ReportServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String op = req.getParameter("op");
        if ("findyear".equals(op)) {
            doFindYear(req,resp);
        } else if ("findmonth".equals(op)) {
            doFindMonth(req,resp);
        } else {
            resp.getWriter().print("WeiGe: no such Methond ...");
        }
    }

    protected void doFindYear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Report report=parseRequest(req,Report.class);
        Map<String,Object> map=service.findyear(report);
        System.out.println("the report:"+report);
        printToJson(resp,map);
    }

    protected void doFindMonth(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Report report=parseRequest(req,Report.class);
        Map<String,Object> map=service.findMonth(report);
        System.out.println("the report:"+report);
        printToJson(resp,map);
    }

}
